package com.huangfu.learning.javabase.java.reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA.
 * Author: huangfu.daohui
 * Date: 2018/7/13
 * Time: 15:24
 * Version: 1.0
 * Description: 查找继承的父类，不能递归查询
 **/
public class GetParentClass {

    public static void main(String[] args) {
        Class<?> clazz = null;
        try {
            clazz = Class.forName("com.huangfu.learning.javabase.java.reflect.Sun");
            Class<?> superClass = clazz.getSuperclass();
            System.out.println("继承的父类为：   " + superClass.getName());//com.huangfu.learning.javabase.java.reflect.Parent
            // 获取方法
            System.out.println(superClass.getMethods()[0]);
            //执行方法
            Method method = superClass.getMethods()[0];
            //invoke 两个参数，第一个是实例，第二个可选，是参数列表，实例构造函数的的参数
            method.invoke(superClass.newInstance());

        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}


class Father {
    public void swimming() {
        System.out.println("Father swimming");
    }
}

class Parent extends Father {
    public void run() {
        System.out.println("parent run");
    }

    public void jump() {
        System.out.println("parent jump");
    }
}

class Sun extends Parent {
    public void run() {
        System.out.println("sun run");
    }

    public void eat() {
        System.out.println("parent eat");
    }
}
